//
//  ButtonAction.m
//  CarHome
//
//  Created by 小伍 on 14-9-19.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "ButtonAction.h"
#import "CollectionCell.h"
#import "Day33Button.h"
#import "UIImageView+WebCache.h"
@class ForumViewController;
@implementation ButtonAction
- (void)dealloc
{
    [_view_add release];
    _view_add = nil;
    [_view_be_add release];
    _view_be_add = nil;
    [_collect_add release];
    _collect_add = nil;
    [_collect_be_add release];
    _collect_be_add = nil;
    [_content_add release];
    [_content_be_add release];
    _content_be_add = nil;
    _content_add = nil;
    [_buttonAction_button release];
    _buttonAction_button = nil;

    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //读文件_add
        NSString * path_add = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        path_add = [NSString stringWithFormat:@"%@/%@",path_add,@"_addd.txt"];
        NSData * data_add = [NSData dataWithContentsOfFile:path_add];
        NSMutableArray * array_add = [NSKeyedUnarchiver unarchiveObjectWithData:data_add];
        ////////////读文件_be_add
        NSString * path_be_add = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        path_be_add = [NSString stringWithFormat:@"%@/%@",path_be_add,@"be_addd.txt"];
        NSData * data_be_add = [NSData dataWithContentsOfFile:path_be_add];
        NSMutableArray * array_be_add = [NSKeyedUnarchiver unarchiveObjectWithData:data_be_add];
        ///////
        
        
        
        
       ////读文件 ,读不到再初始化 33个按钮
        _content_add = [[NSMutableArray alloc]init];
        _content_be_add = [[NSMutableArray alloc]init];
        if ([array_add count] == 0 &&[array_be_add count] == 0) {
            [NetUrl connect:Day33 parmater:nil Block:^(NSData *data) {
                NSMutableDictionary * dic = (NSMutableDictionary *)data;
                NSMutableDictionary * dic2 = [dic objectForKey:@"result"];
                NSMutableArray * array3 = [dic2 objectForKey:@"metalist"];
                NSMutableDictionary * array4 = [array3 objectAtIndex:4];
                NSMutableArray * array5 = [array4 objectForKey:@"list"];
                for (NSMutableDictionary * dic5 in array5) {
                    Day33Button * model = [[Day33Button alloc]init];
                    model.name33 = [dic5 objectForKey:@"name"];
                    model.type = [dic5 objectForKey:@"type"];
                    model.value = [dic5 objectForKey:@"value"];
                    [_content_add addObject:model];
                }
                
            }];
        }
        else
        {
            [_content_add addObjectsFromArray:array_add];
        }
        //把读到得内容加到被添加数组里
        if ([array_be_add count] != 0) {
            [_content_be_add addObjectsFromArray:array_be_add];
        }
      

        [self creatButtonAction];
        
    }
    return self;
}
- (void)creatButtonAction
{
    _view_add = [[UIView alloc]initWithFrame:CGRectMake(0,64+30, self.frame.size.width, self.frame.size.height  - 30)];
    [_view_add setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:_view_add];

    
    ///////第一个collection///////////
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setMinimumInteritemSpacing:10];
    [layout setMinimumLineSpacing:10];
    layout.headerReferenceSize = CGSizeMake(320, 5);
    layout.footerReferenceSize = CGSizeMake(320, 5);
    
    _collect_be_add = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320,50+ [_content_be_add count]/4 * 40) collectionViewLayout:layout];
    _collect_be_add.delegate = self;
    _collect_be_add.dataSource = self;
    [_collect_be_add setBackgroundColor:[UIColor whiteColor]];
    [_collect_be_add registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cell"];
    
    [_view_add addSubview:_collect_be_add];
    
    //////////////第二个collectionView//////////
    UICollectionViewFlowLayout * layout2 = [[UICollectionViewFlowLayout alloc]init];
    [layout2 setMinimumInteritemSpacing:10];
    [layout2 setMinimumLineSpacing:10];
    layout2.headerReferenceSize = CGSizeMake(320, 10);
    layout2.footerReferenceSize = CGSizeMake(320, 50);
    _collect_add = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50+[_content_be_add count]/4 * 40, 320, self.frame.size.height - 64 - 30 - 49 - 40) collectionViewLayout:layout2];
    _collect_add.delegate = self;
    _collect_add.dataSource = self;
    [_collect_add setBackgroundColor:[UIColor whiteColor]];
    [_collect_add registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cell2"];
    
    [_view_add addSubview:_collect_add];
    [layout release];
    [layout2 release];
    //////////////buttton/////////////
    _buttonAction_button = [[UIButton alloc]initWithFrame:CGRectMake(0,self.frame.size.height  - 30 - 40 - 64 - 20 , 320, 60)];
    [_buttonAction_button setTitle:@"确认保存" forState:UIControlStateNormal];
    UIColor * color = myColor_dark;
    _buttonAction_button.titleLabel.font = [UIFont systemFontOfSize:24];
    [_buttonAction_button setBackgroundColor:color];
    [_view_add addSubview:_buttonAction_button];

}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    return CGSizeMake(70, 30);
}
//定义每个UICollectionView 的 margin
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(5, 5, 5, 5);
//}
#pragma mark -
#pragma mark CollectionView cell数目
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _collect_add) {
        return [_content_add count];
    }
    if (collectionView == _collect_be_add) {
        return [_content_be_add count];
    }
    return 0;
    
}
#pragma mark -
#pragma mark CollectionView 重建池
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (collectionView == _collect_add) {
        CollectionCell * cell_add = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        Day33Button * model = [_content_add objectAtIndex:indexPath.row];
        [cell_add.label setText:model.name33];
        cell_add.label.font = [UIFont systemFontOfSize:12];
        [cell_add.label setTextColor:[UIColor blackColor]];
        [cell_add setBackgroundColor:[UIColor whiteColor]];
        //[cell_add.label setBackgroundColor:[UIColor whiteColor]];
        cell_add.layer.cornerRadius = 13;
        cell_add.layer.borderColor = [UIColor blackColor].CGColor;
        cell_add.layer.borderWidth = 1;
        return cell_add;
    }
    if (collectionView == _collect_be_add) {
        CollectionCell * cell_be_add = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        Day33Button * model = [_content_be_add objectAtIndex:indexPath.row];
        [cell_be_add.label setText:model.name33];
        UIColor * color = myColor_dark;
        [cell_be_add setBackgroundColor:color];
        [cell_be_add.label setTextColor:[UIColor whiteColor]];
        //[cell_be_add.label setBackgroundColor:[UIColor grayColor]];
        cell_be_add.label.font = [UIFont systemFontOfSize:12];
        cell_be_add.layer.cornerRadius = 13;
        cell_be_add.layer.borderColor = [UIColor blackColor].CGColor;
        cell_be_add.layer.borderWidth = 1;
        
        return cell_be_add;
    }
    return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collect_add) {
        [_content_be_add addObject:[_content_add objectAtIndex:indexPath.item]];
        [_content_add removeObjectAtIndex:indexPath.item];
        //被添加的东西出现新的一行的时候执行
        if ([_content_be_add count] % 4 == 1) {
            
            [_collect_be_add setFrame:CGRectMake(0, 0, 320,50+ [_content_be_add count]/4 * 40)];
            [_collect_add setFrame:CGRectMake(0, 50 +[_content_be_add count]/4 * 40, 320, [_content_add count]/4 * 40)];

        }
        
        [_collect_add reloadData];
        [_collect_be_add reloadData];
    }
    
    if (collectionView == _collect_be_add) {
        
        [_content_add addObject:[_content_be_add objectAtIndex:indexPath.item]];
        [_content_be_add removeObjectAtIndex:indexPath.item];
        //被添加行数消失一行的时候
            if ([_content_be_add count] % 4 == 0) {
                [_collect_add setFrame:CGRectMake(0, [_content_be_add count]/4 * 40, 320, 50+[_content_add count]/4 * 40)];
            }
                [_collect_be_add setFrame:CGRectMake(0, 0, 320,50+ [_content_be_add count]/4 * 40)];

        [_collect_add reloadData];
        [_collect_be_add reloadData];
        
    }
    
    


}
//- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath didMoveToIndexPath:(NSIndexPath *)toIndexPath
//{
//    UIImage *image = [_content_add objectAtIndex:fromIndexPath.item];
//    [_content_add removeObjectAtIndex:fromIndexPath.item];
//    [_content_add insertObject:image atIndex:toIndexPath.item];
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
